from gameset.phoenicia.base import *

from random import randint


class Immigrant(Seeker):
    color  =_ (Red.dark)
    speed  =_ (4)
    people = Attribute(writable = True)
    
    def on_idle(self):
        if self.home is None or self.home.is_full:
            if self.city.free_housing == 0:
                self.move_to(self.city.random(Signpost))
            else:
                self.home = self.city.random(House)
        else:
            self.move_home()
    
    def on_dead_end(self):
        self.destroy()
    
    def on_arrival_home(self):
        transfer = min(self.home.space_left, self.people)
        
        self.home.add_people(transfer)
        self.people -= transfer
        
        if self.people == 0:
            self.destroy()
    
    def on_arrival_away(self):
        self.destroy()


class Signpost(Building):
    name    =_ ('Signpost')
    color   =_ (Burlywood)
    
    is_passable =_ (True)
    is_walkable =_ (True)
    is_eternal  =_ (True)
    
    def day(self, day, month, year):
        if self.city.free_housing > 0 and self.one_in(self.city.count(Immigrant) * 2 + 1):
            for _ in range(randint(1, self.city.free_housing / 8 + 1)):
                self.spawn(Immigrant, people = randint(1, 8), home = None)
    
    @property
    def spawn_point(self):
        return self.position
